/* 
 * Copyright (c) 2004-2005 Massachusetts Institute of Technology. This code was
 * developed as part of the Haystack (http://haystack.lcs.mit.edu/) research 
 * project at MIT. Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation files (the 
 * "Software"), to deal in the Software without restriction, including without 
 * limitation the rights to use, copy, modify, merge, publish, distribute, 
 * sublicense, and/or sell copies of the Software, and to permit persons to whom
 * the Software is furnished to do so, subject to the following conditions: 
 * 
 * The above copyright notice and this permission notice shall be included in 
 * all copies or substantial portions of the Software. 
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER  
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE. 
 */
/*
 * Created on Jun 13, 2004
 *
 */
package edu.mit.csail.relo.parts;

import java.io.IOException;

import org.eclipse.draw2d.IFigure;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.Request;
import org.eclipse.gef.RequestConstants;
import org.eclipse.gef.tools.DirectEditManager;
import org.eclipse.jface.viewers.TextCellEditor;
import org.openrdf.model.URI;
import org.openrdf.rio.RdfDocumentWriter;
import org.openrdf.vocabulary.RDF;

import edu.mit.csail.relo.ReloCore;
import edu.mit.csail.relo.figures.CommentFigure;
import edu.mit.csail.relo.parts.AnnoLabelDirectEditPolicy.AnnoLabelSource;
import edu.mit.csail.relo.store.ReloRdfRepository;
import edu.mit.csail.relo.store.StoreUtil;

/**
 * @author vineet
 *  
 */
public class CommentEditPart extends MoreItemsEditPart implements AnnoLabelSource {

    private static final URI createReloURI(String str) {
    	return StoreUtil.createMemURI(ReloRdfRepository.reloRdfNamespace + str);
    }

    public static final URI commentTxt = createReloURI("core#commentTxt");

    @Override
    protected void writeRDF(RdfDocumentWriter rdfWriter) throws IOException {
        super.writeRDF(rdfWriter);
		rdfWriter.writeStatement(
		        viewRes, 
		        commentTxt, 
		        StoreUtil.createMemLiteral(getAnnoLabelText()) );
        rdfWriter.writeStatement(getArtifact().elementRes, StoreUtil.createMemURI(RDF.TYPE), ReloCore.commentType);
    }

    @Override
    protected void readRDF(ReloRdfRepository queryRepo) {
        // need to write basic data before we reposition and initialize figure (in base class)
        setAnnoLabelText(queryRepo.getStatement(viewRes, commentTxt, null).getObject().toString());
        //logger.debug("After setText: " + comment.getText());
        
        super.readRDF(queryRepo);
    }

    public IFigure getAnnoLabelFigure() {
        return getFigure();
    }

    public static final String defaultComment = "<<Comment Text>>";
    
    private String text = defaultComment;
    public void setAnnoLabelText(String text) {
        this.text = text;
        refresh();
    }
    public String getAnnoLabelText() {
        return text;
    }

    @Override
    protected void refreshVisuals() {
        super.refreshVisuals();
        ((CommentFigure) getFigure()).setText(getAnnoLabelText());
    }
    
    
    @Override
    protected IFigure createFigure(IFigure curFig, int newDL) {
        return new CommentFigure();
    }

    @Override
    protected void createEditPolicies() {
        super.createEditPolicies();
        installEditPolicy(EditPolicy.DIRECT_EDIT_ROLE, new AnnoLabelDirectEditPolicy(this));
    }

    
    protected DirectEditManager manager;

    protected void performDirectEdit() {
        if (manager == null)
            manager = new AnnoLabelDirectEditManager(
                            this, 
                            TextCellEditor.class, 
                            new AnnoLabelCellEditorLocator(getAnnoLabelFigure()), 
                            getAnnoLabelFigure());
        manager.show();
    }

    @Override
    public void performRequest(Request request) {
        if (request.getType() == RequestConstants.REQ_DIRECT_EDIT)
            performDirectEdit();
        else if (request.getType() == RequestConstants.REQ_OPEN)
            performDirectEdit();
        else
            super.performRequest(request);
    }

    /**
     * @see org.eclipse.gef.EditPart#setSelected(int)
     */
    @Override
    public void setSelected(int value) {
        super.setSelected(value);

        if (value == EditPart.SELECTED_PRIMARY && getAnnoLabelText() == defaultComment) {
            performDirectEdit();
        }

    }

}